package com.ww.idiom.activity.menu;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ww.core.widget.slidemenu.BaseSlidingFragmentActivity;
import com.ww.core.widget.slidemenu.SlidingMenu;
import com.ww.core.widget.slidemenu.MyTitleAdapter;
import com.ww.idiom.R;
import com.ww.idiom.activity.AboutActivity;
import com.ww.idiom.activity.FeedBackActivity;
import com.ww.idiom.activity.MainActivity;
import com.ww.idiom.activity.StoreActivity;

public class LeftFragment extends Fragment {
	private TextView title1;
	public static int press;
	private ListView lv_set;
	private SlidingMenu mSlidingMenu;
	private FragmentTransaction ft;
	private View view;
	private Activity activity;
	private MyTitleAdapter adapter;
	public static String title[] = { "首页", "收藏", "反馈", "关于" };
	private Fragment mFrag[] = { new MainActivity(), new StoreActivity(), new FeedBackActivity(), new AboutActivity() };
	public static String mTitleName = title[0];
	private int item[] = new int[title.length];
	private int clickFlag = 0;
	private int[] imgs = { R.drawable.html_home, R.drawable.market_recommend_button,
			R.drawable.menu_bookshelf_feedback, R.drawable.menu_bookshelf_about };
	private int[] imgsOn = { R.drawable.html_home, R.drawable.market_recommend_button,
			R.drawable.menu_bookshelf_feedback, R.drawable.menu_bookshelf_about };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.slidemenu_left_fragment, container, false);
		initView(view);
		return view;
	}

	public void initView(View view) {
		// title
		title1 = (TextView) view.findViewById(R.id.menu_title);
		title1.setText("菜单");

		lv_set = (ListView) view.findViewById(R.id.lv_set);
		lv_set.setAdapter(new ArrayAdapter<String>(this.getActivity(), R.layout.slidemenu_item, R.id.tv_item, title));
		adapter = new MyTitleAdapter(title, null, LeftFragment.this.getActivity(), imgs, imgsOn);
		lv_set.setAdapter(adapter);
		lv_set.setOnItemClickListener(new mItemClickListener());

		activity = this.getActivity();
		ft = ((FragmentActivity) activity).getSupportFragmentManager().beginTransaction();
		mFrag[0] = new MainActivity();
		press = 0;
		ft.replace(R.id.main_center_fragment, mFrag[0]);
		ft.commit();
	}

	class mItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			adapter.setSelectedIndex(position);
			adapter.notifyDataSetChanged();
			ft.remove(mFrag[0]);
			ft = ((FragmentActivity) activity).getSupportFragmentManager().beginTransaction();
			if (item[position] != position || clickFlag == position) {
				ft.remove(mFrag[position]);
				if (position == 0) {
					mFrag[position] = new MainActivity();
				} else if (position == 1) {
					mFrag[position] = new StoreActivity();
				}
				if (position == 2) {
					mFrag[position] = new FeedBackActivity();
				}
				if (position == 3) {
					mFrag[position] = new AboutActivity();
				}
				ft.add(R.id.main_center_fragment, mFrag[position]);
			}
			for (int i = 0; i < title.length; i++) {
				ft.hide(mFrag[i]);
			}
			ft.show(mFrag[position]);
			press = position;
			ft.commit();
			item[position] = position;
			clickFlag = position;

			BaseSlidingFragmentActivity baseFragment = (BaseSlidingFragmentActivity) getActivity();
			mSlidingMenu = baseFragment.getSlideMenu();
			mSlidingMenu.showContent();

			mTitleName = title[position];
		}
	}

	public void toMain() {
		adapter.setSelectedIndex(0);
		adapter.notifyDataSetChanged();
		ft.remove(mFrag[0]);
		ft = ((FragmentActivity) activity).getSupportFragmentManager().beginTransaction();
		ft.remove(mFrag[0]);
		mFrag[0] = new MainActivity();
		ft.add(R.id.main_center_fragment, mFrag[0]);
		for (int i = 0; i < title.length; i++) {
			ft.hide(mFrag[i]);
		}
		ft.show(mFrag[0]);
		press = 0;
		ft.commit();
		item[0] = 0;
		clickFlag = 0;

		BaseSlidingFragmentActivity baseFragment = (BaseSlidingFragmentActivity) getActivity();
		mSlidingMenu = baseFragment.getSlideMenu();
		mSlidingMenu.showContent();

		mTitleName = title[0];
	}

}
